package org.andao.commons.service;

import java.util.List;
import java.util.Map;

import org.andao.commons.model.Message;
import org.andao.commons.model.MessageReceiver;
import org.andao.commons.model.MessageSender;
import org.andao.commons.model.MessageStat;
import org.andao.core.base.service.BaseService;
import org.andao.core.dao.helper.Page;
import org.andao.core.exception.ServiceException;
import org.andao.security.model.OrgUnit;
import org.andao.security.model.User;



/**
 * 系统消息服务.
 * @author Marco.hu(hzg139@163.com)
 *
 */
public interface SysMsgService extends BaseService<Message>{
	
	/**
	 * 保存消息
	 * @param messageSender
	 * @param messageReceiver
	 * @param attachIds
	 * @throws ServiceException
	 */
	void saveMessage(MessageSender messageSender,MessageReceiver messageReceiver,String[] attachIds) throws ServiceException;
	/**
	 * 更新消息
	 * @param messageSender
	 * @param messageReceiver
	 * @param attachIds
	 * @throws ServiceException
	 */
	void updateMessage(MessageSender messageSender,MessageReceiver messageReceiver,String[] attachIds) throws ServiceException;
	/**
	 * 删除消息
	 * @param type
	 * @param ids
	 * @throws ServiceException
	 */
	void deleteMessage(String type,String[] ids) throws ServiceException;
	/**
	 * 当前用户未读消息数目
	 * @return
	 * @throws ServiceException
	 */
	Integer unreadMessageCount(User user) throws ServiceException;
	
	/**
	 * 保存阅读痕迹
	 * @param messageStat
	 * @throws ServiceException
	 */
	void saveMessageStat(MessageStat messageStat) throws ServiceException;
	
	/**
	 * 发送消息 
	 * @param receivers     接收者     : 按组织(组织编码)/按角色(角色编码)/按用户(用户名)
	 * @param receiveType   接收类型     按组织/ 按角色/按用户
	 * @param sendUser      发送者
	 * @param msgTitle      消息标题
	 * @param msgContent    消息 key是接收者，value是消息内容
	 * @param msgType       消息类型:  tips - 温馨提示 / sysmsg - 系统消息/ usermsg - 用户消息
	 * @param isdraft       是否草稿   Y/N
	 * @param isReply       消息是否允许回复 Y/N
	 * @param formType      表单类型
	 * @param formId        表单ID
	 * @throws ServiceException
	 */
	void sendMsgByIds(List<String> receivers,String receiveType,User sendUser,Map<String, String> msgTitle,Map<String, String> msgContent,String msgType,String isdraft ,String isReply,String formType,String formId)throws ServiceException;
	
	/**
	 *  发送消息
	 * @param sendUserId        消息发送者用户ID
	 * @param sendUserCnName    消息发送者用户中文名
	 * @param sendUsersOrgUnit  消息发送者所属的组织单位
	 * @param msgType   		消息类型
	 * @param title     		消息标题
	 * @param msg       		消息内容
	 * @param receiverUserName  消息接收者
	 * @param receiveType       消息的接收类型:按用户、组织...
	 * @param formId  			消息发送者所属的formId
	 * @param formType  		消息发送者所属的formType
	 */
	void sendMsg(String sendUserId,String sendUserCnName,OrgUnit sendUsersOrgUnit,String msgType,String title,
				 String msg,String receiverUserName,String receiveType,String formId,String formType)throws ServiceException;
	
	/**
	 * 用户是否已阅读
	 * @param readUserId
	 * @return
	 * @throws ServiceException
	 */
	boolean isMessageReaded(String msgId,String readUserId) throws ServiceException;
	
	/**
	 * 獲取收件人的消息ID
	 * @param userId
	 * @return
	 * @throws ServiceException
	 */
	List<String> getIsReadMsgIds(String userId) throws ServiceException;
	
	/**
	 * 分页查询已发送消息
	 * @param condition
	 * @param page
	 * @return
	 * @throws ServiceException
	 */
	Page findSendedMessageByCondition(Map<String,Object> condition,Page page) throws ServiceException;
	
	/**
	 * 分页查询接受到的消息
	 * @param condition
	 * @param page
	 * @return
	 * @throws ServiceException
	 */
	Page findReceiveMessageByCondition(Map<String,Object> condition,Page page) throws ServiceException;
	
	/**
	 * 获取用户消息列表.
	 * @param condition userName , unitCode,roleCode,fetchSize,userId
	 * @return
	 * @throws ServiceException
	 */
	List<Map<String, Object>> getUserMessageList(Map<String, Object> condition) throws ServiceException;
	
	/**
	 * 转换ID或code为名称
	 * @param messageReceiver
	 * @throws ServiceException
	 */
	MessageReceiver codesOrId2Name(MessageReceiver messageReceiver) throws ServiceException;
}
